package de.ama.view;

import de.ama.crawler.Entry;
import de.ama.crawler.Store;
import de.ama.util.BrowserLaunch;
import de.ama.util.Util;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;

public class DetailPanel extends JPanel {
    private Box mainBox = Box.createHorizontalBox();
    private Box cmdBox = Box.createHorizontalBox();
    private Box rightBox = Box.createVerticalBox();

    private JTextArea text = new JTextArea();
    private JTextArea plot = new JTextArea();
    private JLabel poster = new JLabel();
    private JTextField searchInput = new JTextField();
    private JLabel startMovie = new JLabel(Util.loadIcon("windows-play-icon.png"), SwingConstants.RIGHT);
    private JLabel startWiki = new JLabel("Wiki", Util.loadIcon("Wikipedia-32.png"), SwingConstants.RIGHT);
    private JLabel startMoviedb = new JLabel("MoviDB", Util.loadIcon("moviedb.png"), SwingConstants.RIGHT);
    private JLabel startGoogle = new JLabel("Google", Util.loadIcon("Google_32x32.png"), SwingConstants.RIGHT);
    private JLabel startYoutube = new JLabel("Trailer", Util.loadIcon("YouTube-32.png"), SwingConstants.RIGHT);
    private Dialog dlg;
    private JTextComponent lastFocusedComponent = null;

    public DetailPanel(final Dialog dlg) {
        this.dlg = dlg;
        setBorder(null);
        dlg.initComponent(this);
        dlg.initComponent(mainBox);
        dlg.initComponent(rightBox);
        dlg.initComponent(poster);
        dlg.initComponent(plot);
        dlg.initComponent(text);
        dlg.initComponent(searchInput);
        dlg.initComponent(startMovie);
        dlg.initComponent(startWiki);
        dlg.initComponent(startMoviedb);
        dlg.initComponent(startGoogle);
        dlg.initComponent(startYoutube);

        searchInput.setMaximumSize(new Dimension(490, 20));

        startMovie.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                AppWindow.get().startMovie(dlg.getEntry());
            }
        });

        JPopupMenu menu = new JPopupMenu();
        Util.addMenuItem(menu, "paste","icons/paste_plain.png", new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                Image image = Util.getClipboardImage();
                if (image != null) {
                    poster.setIcon(new ImageIcon(image));
                    try {
                        Store.get().storePoster(image, dlg.getEntry());
                    } catch (Exception e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
            }
        });

        Util.addPopupMenu(poster, menu);

        plot.addFocusListener(new FocusHandler());
        text.addFocusListener(new FocusHandler());
        searchInput.addFocusListener(new FocusHandler());

        startWiki.addMouseListener(new MouseHandler("www.wikipedia.org/wiki/", "_"));
        startGoogle.addMouseListener(new MouseHandler("www.google.de/search?&q=", "+"));
        startMoviedb.addMouseListener(new MouseHandler("www.themoviedb.org/search?search=", "+"));
        startYoutube.addMouseListener(new MouseHandler("www.youtube.com/results?search_query=", "+"));

        mainBox.add(poster);
        mainBox.add(Box.createHorizontalStrut(5));
        mainBox.add(rightBox);
        rightBox.add(Box.createVerticalStrut(5));
        rightBox.add(plot);
        rightBox.add(Box.createVerticalStrut(5));
        rightBox.add(text);
        rightBox.add(Box.createVerticalStrut(5));
        rightBox.add(searchInput);
        rightBox.add(Box.createVerticalStrut(5));
        cmdBox.add(startWiki);
        cmdBox.add(Box.createHorizontalStrut(20));
        cmdBox.add(startMoviedb);
        cmdBox.add(Box.createHorizontalStrut(20));
        cmdBox.add(startYoutube);
        cmdBox.add(Box.createHorizontalStrut(20));
        cmdBox.add(startGoogle);
        cmdBox.add(Box.createHorizontalStrut(20));
        cmdBox.add(startMovie);
        cmdBox.add(Box.createHorizontalStrut(20));
        rightBox.add(cmdBox);
        add(mainBox);

        initKeyListeners();
    }

    private void initKeyListeners() {
        searchInput.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String text = searchInput.getText();
                if ("del".equals(text)) {
                    if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(DetailPanel.this, "Delete ?")) {
                        Store.get().removeEntry(dlg.getEntry());
                        dlg.onCancel();
                    }
                } else {
                    dlg.searchAgain(text);
                }
            }
        });

        mainBox.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dlg.onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    }


    public void showData(Entry entry) {
        poster.setIcon(Util.loadIcon(entry.getPosterPath()));
        plot.setText(entry.getPlot());
        text.setText(entry.getText());
        dlg.setTitle(entry.getTitle() + "    " + entry.getPath());
        searchInput.setText(entry.getSearchString());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                searchInput.requestFocus();
            }
        });
    }


    private class FocusHandler implements FocusListener {
        @Override
        public void focusGained(FocusEvent e) {
            lastFocusedComponent = (JTextComponent) e.getComponent();

        }

        @Override
        public void focusLost(FocusEvent e) {
            lastFocusedComponent = (JTextComponent) e.getComponent();
        }
    }

    private class MouseHandler extends MouseAdapter {
        private String pre;
        private String del;

        private MouseHandler(String pre, String del) {
            this.pre = pre;
            this.del = del;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            String search = pre;
            String selectedText = lastFocusedComponent.getSelectedText();
            selectedText = Util.safeToString(selectedText, searchInput.getText());
            if (!Util.isEmpty(selectedText)) {
                search += selectedText.trim().replace(" ", del);
                BrowserLaunch.openURL(search);
            }
        }
    }
}
